package com.ellecity06.cwhiskymall.view.dialog

import android.view.Gravity
import android.view.LayoutInflater
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.ellecity06.common.base.BaseDialog
import com.ellecity06.common.ktx.requestStorePermission
import com.ellecity06.common.utils.BitmapUtils
import com.ellecity06.common.utils.FileUtil
import com.ellecity06.common.utils.ToastUtils
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.databinding.DialogInviteShareBinding

/**
 * @author ellecity06
 * @time 2024/11/5 14:14
 * @des
 */
class ShareInviteDialog(val activity: FragmentActivity, private val shareUrl: String) :
    BaseDialog<DialogInviteShareBinding>(activity) {
    override val widthP: Float
        get() = 0.84F
    override val gravity: Int
        get() = Gravity.TOP
    override val animation: Int
        get() = R.style.animScaleCenter
    override fun inflaterViewBinding(): DialogInviteShareBinding {
        return DialogInviteShareBinding.inflate(LayoutInflater.from(context), null, false)
    }

    override fun initialize() {
        binding.tvSave.setOnClickListener {
            try {
                val bitmapFromView = BitmapUtils.createBitmapFromView(binding.layoutTop)
                activity.requestStorePermission(desc = "保存分享图需要您授予文件权限") { allGranted, grantedList, deniedList ->
                    if (allGranted) {
                        FileUtil.saveImageFileToExternal(
                            activity,
                            imageName = FileUtil.getRandomImageName(),
                            imageBitmap = bitmapFromView
                        )
                        ToastUtils.show("保存成功")
                        dismiss()
                    }
                }

            } catch (e: Exception) {
                ToastUtils.show("保存失败")
            }

        }
        val drawable = ContextCompat.getDrawable(activity, R.mipmap.icon_qr_logo)
        if (drawable != null) {
            val qrBitmap = BitmapUtils.createQrBitmap(shareUrl, 105F, drawable)
            binding.ivCode.setImageBitmap(qrBitmap)
        }
    }
}